package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.tanhua.common.pojo.Settings;
import com.tanhua.common.mapper.SettingsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @Autowired
    private SettingsMapper settingsMapper;

    /**
     *  查询推送喜欢、评论以及公告通知
     * @return
     */
    public Settings querySettings(Long userId){
        // 根据userId在mysql数据库查询推送喜欢、评论以及公告通知
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        Settings settings = this.settingsMapper.selectOne(queryWrapper);

        return settings;
    }

    /**
     *  更新通知
     *
     * @param userId
     * @param likeNotification
     * @param pinglunNotification
     * @param gonggaoNotification
     */
    public void updateNotification(Long userId, Boolean likeNotification, Boolean pinglunNotification, Boolean gonggaoNotification) {
        // 查询通知
        Settings settings = this.querySettings(userId);

        if(null == settings){
            // 如果通知为空则插入
            settings = new Settings();
            settings.setId(userId);
            this.settingsMapper.insert(settings);
        }

        // 如果通知不为空则更新
        settings.setLikeNotification(likeNotification);
        settings.setPinglunNotification(pinglunNotification);
        settings.setGonggaoNotification(gonggaoNotification);

        this.settingsMapper.updateById(settings);
    }
}
